// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SourceFile

package com.sgame.android.social;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.Vector;

import com.sgame.android.MLog;
import com.sgame.android.lang.Error;
import com.sgame.android.lang.ErrorMap;
import com.sgame.android.social.IMobageHttpResponseHandler;
import com.sgame.android.data.MBUParcelableUser;
import com.sgame.android.data.MBUUser;
import com.sgame.android.data.User;
import com.sgame.android.network.ConcurrentHashMapInstance;
import com.sgame.android.network.HttpRequest;
import com.sgame.android.network.ISgameHttpClient;
import com.sgame.android.network.SgameHttpClient;
import com.sgame.android.network.SgameJsonHttpResponseHandler;
import com.sgame.android.network.InvalidCredentialsConfigurationException;
import com.sgame.android.network.util.JsonString;
import com.sgame.android.network.util.MakeCmdUrl;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class SgameJsonHttpRequestExecute
{
	

	public static void sendFriendInviteToEmail(String email, ISgameHttpClient parame, String userId, String appKey, IMobageHttpResponseHandler.OnSendFriendInviteToEmailIfPossibleResponseHandler handler)
		throws InvalidParameterException, InvalidCredentialsConfigurationException
	{
		String url;
		final IMobageHttpResponseHandler.OnSendFriendInviteToEmailIfPossibleResponseHandler localHandler = handler;
		if ((email == null) || (email.length() == 0))
			throw new InvalidParameterException();
		SgameHttpClient httpClient = parame.createHttpClient(1);
		HttpRequest localHttpRequest;
		(localHttpRequest = new HttpRequest()).setMethod("POST");
		JSONObject localJSONObject = new JSONObject();
		JsonString locale = null;
		try
		{
			localJSONObject.put(email, parame);
			locale = new JsonString(localJSONObject.toString());
		}
		catch (JSONException localJSONException)
		{
			localJSONException.printStackTrace();
		}
		catch (UnsupportedEncodingException localUnsupportedEncodingException)
		{
			localUnsupportedEncodingException.printStackTrace();
		}
		localHttpRequest.setBody(locale);
		url = MakeCmdUrl.sendEmail(appKey, userId);
		httpClient.setUrl(url);
		MLog.d(userId, "sendFriendInviteToEmailIfPossible url:" + url);
		httpClient.Execute(localHttpRequest, new SgameJsonHttpResponseHandler(handler)
		{
			public final void onSuccess(JSONObject paramAnonymousJSONObject)
			{
				MLog.d("MobageJsonHttpResponseHandler", "sendFriendInviteToEmailIfPossible response from server:" + paramAnonymousJSONObject);
				localHandler.onComplete();
			}

			public final void onFailure(Throwable paramAnonymousThrowable, String paramAnonymousString)
			{
				MLog.e("MobageJsonHttpResponseHandler", "sendFriendInviteToEmailIfPossible request failure due to not connecting to the server: " + paramAnonymousThrowable.getMessage());
				localHandler.onError(new Error(ErrorMap.NETWORK_UNAVAILABLE, paramAnonymousThrowable));
			}

			public final void onFailure(Error paramAnonymousError, JSONObject paramAnonymousJSONObject)
			{
				MLog.e("MobageJsonHttpResponseHandler", "sendFriendInviteToEmailIfPossible request failure" + paramAnonymousError.getCode() + paramAnonymousError.getDescription());
				localHandler.onError(paramAnonymousError);
			}
		});
	}

	public static void getCurrentUser(ISgameHttpClient parame, String userId, String appKey, final IMobageHttpResponseHandler.OnGetUserResponseHandler handler)
		throws InvalidCredentialsConfigurationException
	{
		SgameHttpClient httpClient = parame.createHttpClient(1);
		HttpRequest localHttpRequest;
		localHttpRequest = new HttpRequest();
		localHttpRequest.setMethod("GET");
		MBUUser.Field[] fields = getFieldArray();
		if (fields != null)
		{
			localHttpRequest.addQueryParam("fields", getFieldsList(fields));
		}

		String url = MakeCmdUrl.getUserCurrent(appKey, userId);
		httpClient.setUrl(url);

		MLog.d(appKey, "getCurrentUser url:" + url);

		httpClient.Execute(localHttpRequest, new SgameJsonHttpResponseHandler()
		{
			public final void onSuccess(JSONObject paramAnonymousJSONObject)
			{
				MLog.d("MobageJsonHttpResponseHandler", "getCurrentUser response from server:" + paramAnonymousJSONObject);
				MBUUser user = MBUUser.getFromJson(paramAnonymousJSONObject);
				handler.onComplete((User)user);
			}

			public final void onFailure(Throwable paramAnonymousThrowable, String paramAnonymousString)
			{
				MLog.e("MobageJsonHttpResponseHandler", "getCurrentUser request failure due to not connecting to the server: " + paramAnonymousThrowable.getMessage());
				handler.onError(new Error(ErrorMap.NETWORK_UNAVAILABLE, paramAnonymousThrowable));
			}

			public final void onFailure(Error paramAnonymousError, JSONObject paramAnonymousJSONObject)
			{
				MLog.e("MobageJsonHttpResponseHandler", "getCurrentUser request failure" + paramAnonymousError.getCode() + paramAnonymousError.getDescription());
				handler.onError(paramAnonymousError);
			}
		});
	}

	public static void getFriends(String friendId, PagingOption paramPagingOption, ISgameHttpClient parame, String userId, String appKey, final IMobageHttpResponseHandler.OnGetUsersResponseHandler handler)
			throws InvalidParameterException, InvalidCredentialsConfigurationException
	{
		if ((friendId == null) || (friendId.length() == 0))
		{
			throw new InvalidParameterException();
		}

		SgameHttpClient httpClient = parame.createHttpClient(1);
		HttpRequest localHttpRequest;
		localHttpRequest = new HttpRequest();
		localHttpRequest.setMethod("GET");
		MBUUser.Field[] fields = getFieldArray();
		if (fields != null)
		{
			localHttpRequest.addQueryParam("fields", getFieldsList(fields));
		}

		if (paramPagingOption != null)
		{
			localHttpRequest.addQueryParam("count", Integer.toString(paramPagingOption.count));
			localHttpRequest.addQueryParam("startIndex", Integer.toString(paramPagingOption.start));
		}

		String url = MakeCmdUrl.getUserFriends(appKey, userId, friendId);
		MLog.d(userId, "getFriends url:" + url);
		httpClient.setUrl(url);

		httpClient.Execute(localHttpRequest, new SgameJsonHttpResponseHandler()
		{
			public final void onSuccess(JSONObject jsobj)
			{
				ArrayList localArrayList = new ArrayList();
				PageOptionsResult localPageOptionsResult = null;
				JSONArray array;
				MLog.d("MobageJsonHttpResponseHandler", "getFriends response from server:" + jsobj);
				try
				{
					localPageOptionsResult = SgameJsonHttpRequestExecute.a(jsobj);
					array = localPageOptionsResult.getEntry();
					for (int i = 0; i < array.length(); i++) {
			            localArrayList.add(MBUUser.getFromJson(array.getJSONObject(i)));
					}
				}
				catch (JSONException ex)
				{
					Object localObject = new Error(ErrorMap.SERVER_UNEXPECTED_RESPONSE, ex);
					handler.onError((Error)localObject);
				}

				handler.onComplete(localArrayList, localPageOptionsResult);
			}

			public final void onFailure(Throwable paramAnonymousThrowable, String paramAnonymousString)
			{
				MLog.e("MobageJsonHttpResponseHandler", "getFriends request failure due to not connecting to the server: " + paramAnonymousThrowable.getMessage());
				handler.onError(new Error(ErrorMap.NETWORK_UNAVAILABLE, paramAnonymousThrowable));
			}

			public final void onFailure(Error paramAnonymousError, JSONObject paramAnonymousJSONObject)
			{
				MLog.e("MobageJsonHttpResponseHandler", "getFriends request failure" + paramAnonymousError.getCode() + paramAnonymousError.getDescription());
				handler.onError(paramAnonymousError);
			}
		});
	}

	public static void getUsers(List<String> paramList, ISgameHttpClient parame, String userId, String appKey, final IMobageHttpResponseHandler.OnGetUsersResponseHandler handler)
		throws InvalidParameterException
	{
		if (paramList == null)
		{
			throw new InvalidParameterException();
		}
		
		if (paramList.isEmpty())
		{
			handler.onComplete(new ArrayList(), null);
			return;
		}
		HttpRequest localHttpRequest = new HttpRequest();
		MBUUser.Field[] fields = getFieldArray();
		if (fields != null)		{
			localHttpRequest.addQueryParam("fields", getFieldsList(fields));
		}

		final UUID localUUID = UUID.randomUUID();
		final int i = paramList.size();
		ConcurrentHashMapInstance.getInstance().put(localUUID, new Vector());
		String user, list = "";
		Iterator<String> item;
		for (item = paramList.iterator(); item.hasNext(); )
		{
			user = item.next();
			list += user + ",";
		}
		SgameHttpClient httpClient;
		try
		{
			httpClient = parame.createHttpClient(1);
			
		String url = MakeCmdUrl.getUser(appKey, userId, list);
		MLog.d(list, "getUsers url:" + url);
		httpClient.setUrl(url);
		
		httpClient.Execute(localHttpRequest, new SgameJsonHttpResponseHandler(handler)
		{
			public final void onSuccess(JSONObject paramAnonymousJSONObject)
			{
				User localUser = null;
				JSONArray array;
				Object localObject2;
				PageOptionsResult localPageOptionsResult = null;
				UserResult userResult = new UserResult();
				localPageOptionsResult = SgameJsonHttpRequestExecute.a(paramAnonymousJSONObject);
				if (localPageOptionsResult != null)
				{
					array = localPageOptionsResult.getEntry();
					localObject2 = new JSONObject();
					
					if (array.length() > 0) {
						try {
							localObject2 = array.getJSONObject(0);
						}
						catch (JSONException ex)
						{
							ex.printStackTrace();
						}
						
						localUser = User.createFromJson((JSONObject)localObject2);
					}
				}
				else {
					new Error(ErrorMap.SERVER_UNEXPECTED_RESPONSE);
					array = null;
				}
				
				userResult.setUser(localUser);
				userResult.setResult(localPageOptionsResult);
				Vector vector = (Vector)ConcurrentHashMapInstance.getInstance().get(localUUID);
				vector.add(userResult);
				
				if (i == vector.size())
				{
					int i = 0;
					localPageOptionsResult = new PageOptionsResult();
					localPageOptionsResult.setStart(1);
					ArrayList localArrayList = new ArrayList();
					Iterator<UserResult> item;
					for (item = vector.iterator(); item.hasNext(); )
					{
						UserResult locala;
						locala = item.next();
						if ((locala.getUser() != null) && (locala.getResult() != null)) {
							localArrayList.add(locala.getUser());
							localPageOptionsResult.setCount(localPageOptionsResult.getCount() + locala.getResult().getCount());
							localPageOptionsResult.setTotal(localPageOptionsResult.getTotal() + locala.getResult().getTotal());
						}
						else
						{
							i = 1;
						} 
					}
					if (i == 0)
					{
						ConcurrentHashMapInstance.getInstance().remove(localUUID);
						handler.onComplete(localArrayList, localPageOptionsResult);
						return;
					}
					ConcurrentHashMapInstance.getInstance().remove(localUUID);
					handler.onError(new Error(ErrorMap.SERVER_UNEXPECTED_RESPONSE));
				}
			}

			public final void onFailure(Throwable paramAnonymousThrowable, String paramAnonymousString)
			{
				MLog.e("MobageJsonHttpResponseHandler", "getUsers request failure due to not connecting to the server: " + paramAnonymousThrowable.getMessage());
				handler.onError(new Error(ErrorMap.NETWORK_UNAVAILABLE, paramAnonymousThrowable));
			}

			public final void onFailure(Error paramAnonymousError, JSONObject paramAnonymousJSONObject)
			{
				MLog.e("MobageJsonHttpResponseHandler", "getUsers request failure" + paramAnonymousError.getCode() + paramAnonymousError.getDescription());
				handler.onError(paramAnonymousError);
			}
			
			class UserResult
			{
				private User mUser;
				private PageOptionsResult mResult;
				
				public UserResult()
				{
				}

				public final User getUser()
				{
					return mUser;
				}
				
				public final void setUser(User paramUser) {
					mUser = paramUser;
				}
				
				public final PageOptionsResult getResult() {
					return mResult;
				}
				
				public final void setResult(PageOptionsResult paramPageOptionsResult) {
					mResult = paramPageOptionsResult;
				}
			}

		});
		}
		catch (InvalidCredentialsConfigurationException ex)
		{
			ex.printStackTrace();
		}
	}

	public static void getFriendsWithGame(String gameId, PagingOption paramPagingOption, ISgameHttpClient parame, String userId, String appKey, final IMobageHttpResponseHandler.OnGetUsersResponseHandler handler)
		throws InvalidParameterException
	{
		if ((gameId == null) || (gameId.length() == 0))
		{
			throw new InvalidParameterException();
		}
		SgameHttpClient httpClient;
		try {
			httpClient = parame.createHttpClient(1);
		}
		catch (InvalidCredentialsConfigurationException ex)
		{
			handler.onError(new Error(ErrorMap.MOBAGE_NOT_INITIALIZED, (Throwable)ex));
			return;
		}
			
		HttpRequest localHttpRequest = new HttpRequest();
		localHttpRequest.setMethod("GET");
		MBUUser.Field[] fields = getFieldArray();
		if (fields != null)		{
			localHttpRequest.addQueryParam("fields", getFieldsList(fields));
		}

		if (paramPagingOption != null)
		{
			localHttpRequest.addQueryParam("count", Integer.toString(paramPagingOption.count));
			localHttpRequest.addQueryParam("startIndex", Integer.toString(paramPagingOption.start));
		}

		localHttpRequest.addQueryParam("filterBy", "hasApp");
		localHttpRequest.addQueryParam("filterOp", "equals");
		localHttpRequest.addQueryParam("filterValue", "true");

		String url = MakeCmdUrl.getUserGameFriends(appKey, userId, gameId);
		MLog.d(userId, "getFriendsWithGame url:" + url);
		httpClient.setUrl(url);
		
		httpClient.Execute(localHttpRequest, new SgameJsonHttpResponseHandler()
		{
			public final void onSuccess(JSONObject resultObj)
			{
				ArrayList localArrayList = new ArrayList();
				PageOptionsResult localPageOptionsResult = null;
				JSONArray array;

				MLog.d("MobageJsonHttpResponseHandler", "getFriendsWithGame response from server:" + resultObj);
				try
				{
					localPageOptionsResult = new PageOptionsResult(resultObj);
					array = localPageOptionsResult.getEntry();
					for (int i = 0; i < array.length(); i++)
					{
						localArrayList.add(MBUUser.getFromJson(array.getJSONObject(i)));
					}
				}
				catch (JSONException ex)
				{
					Object localObject = new Error(ErrorMap.SERVER_UNEXPECTED_RESPONSE, ex);
					handler.onError((Error)localObject);
				}

				handler.onComplete(localArrayList, localPageOptionsResult);
			}

			public final void onFailure(Throwable paramAnonymousThrowable, String paramAnonymousString)
			{
				MLog.e("MobageJsonHttpResponseHandler", "getFriendsWithGame request failure due to not connecting to the server: " + paramAnonymousThrowable.getMessage());
				handler.onError(new Error(ErrorMap.NETWORK_UNAVAILABLE, paramAnonymousThrowable));
			}

			public final void onFailure(Error paramAnonymousError, JSONObject paramAnonymousJSONObject)
			{
				MLog.e("MobageJsonHttpResponseHandler", "getFriendsWithGame request failure" + paramAnonymousError.getCode() + paramAnonymousError.getDescription());
				handler.onError(paramAnonymousError);
			}
		});
	}

	private static PageOptionsResult b(JSONObject paramJSONObject)
	{
		PageOptionsResult localPageOptionsResult = null;
		try
		{
			localPageOptionsResult = new PageOptionsResult(paramJSONObject);
		}
		catch (JSONException localJSONException1)
		{
			try
			{
				ArrayList localArrayList;
				localArrayList = new ArrayList();
				localArrayList.add(new PageOptionsEntry(paramJSONObject));
				localPageOptionsResult = new PageOptionsResult(localArrayList);
			}
			catch (JSONException localJSONException2)
			{
				localJSONException2.printStackTrace();
			}

		}

		return localPageOptionsResult;
	}

	private static MBUUser.Field[] getFieldArray()
	{
		HashSet<MBUUser.Field> localHashSet;
		localHashSet = new HashSet<MBUUser.Field>();
		localHashSet.add(MBUUser.Field.ID);
		localHashSet.add(MBUUser.Field.USER_ID);
		localHashSet.add(MBUUser.Field.NICKNAME);
		localHashSet.add(MBUUser.Field.THUMBNAIL_URL);
		localHashSet.add(MBUUser.Field.DISPLAY_NAME);

		localHashSet.add(MBUUser.Field.HAS_APP);
		localHashSet.add(MBUUser.Field.AGE_RESTRICTED);
		localHashSet.add(MBUUser.Field.AGE);

		return (MBUUser.Field[])localHashSet.toArray(new MBUUser.Field[localHashSet.size()]);
	}

	private static String getFieldsList(MBUUser.Field[] paramArrayOfField)
	{
		String str1 = "";
		String str2 = null;
		for (int i = 0; i < paramArrayOfField.length; i++)
		{
			str2 = paramArrayOfField[i].name();
			if (str2.equalsIgnoreCase("user_id"))
			{
				str2 = "id";
			}
			str1 = str1 + str2 + ",";
		}
		if (!str1.equals(""))
		{
			str1 = str1.substring(0, str1.lastIndexOf(","));
		}
		return str1;
	}

	static PageOptionsResult a(JSONObject jsonobject)
    {
        return b(jsonobject);
    }
}
